package pack1

import "fmt"

func NewTest(){
	var pArr = new([1]int)
	fmt.Printf("pArr type: %T,  address: %p\n",pArr, pArr)
	(*pArr)[0] = 1
	if (*pArr)[0] == 0 {
		fmt.Println("pArr is nil")
	}
}

func MakeTest(){
	slice01 := make([]int, 4)
	slice01[0] = 1
	slice01[1] = 2
	slice01[2] = 3
	slice01[3] = 4
	fmt.Println("slice01:", slice01)
	//for i,v := range slice01 {
	//	fmt.Printf("index: %d, value:%v\n", i, v)
	//}

	seasons := []string{"Spring", "Summer", "Autumn", "Winter"}
	for ix, season := range seasons {
		fmt.Printf("Season %d is: %s\n", ix, season)
	}
	seasons[0] = "changeSpring"
	var season string
	for _, season = range seasons {
		fmt.Printf("%s\n", season)
	}

	seasonsTo := make([]string,10)
	copy(seasonsTo, seasons)
	fmt.Println(seasonsTo)

	sl3 := []string{"a", "b"}
	sl3 = append(sl3, "c", "d")
	fmt.Println("sl3", sl3)

	golang := "golang"
	golangslice := golang[0:len(golang)]
	fmt.Println("golang slice", golangslice)

	//golang1 := append([]string(golang[:1]), golang[1+1])

	sl1 := []int{1,2,3,4}
	sl1 = append(sl1[:1], sl1[2])
	fmt.Println("sl1", sl1)

}

func sum(a []int) int {
	s := 0
	for v := range a {
		s += a[v]
		a[v] = a[v] + 1
	}
	fmt.Println("sum arr:", a)
	return s
}

func Slice02() {
	arr := []int{1, 2}
	fmt.Println("arr:", arr)
	sumValue := sum(arr)

	arr2 := arr
	arr2[0] = 11
	fmt.Println("sumValue:", sumValue, "origin arr:", arr, "arr2:", arr2)
}

func SliceSample01() {
	var slice01 = []int{1, 2, 3}
	fmt.Println(slice01[0:])

	var arr1 [6]int
	arr1[1] = 2
	arr1[5] = 6

	var slice02 = arr1[1:2]
	fmt.Println("slice02", slice02)
}

func ArraySample02() {
	// 数组是的复制是值COPY
	var arr1 [6]int
	for i := 0; i < len(arr1); i++ {
		arr1[i] = i
	}
	//arr1 := [6]int{1, 2, 3, 4, 5, 6}
	fmt.Println("arr1:", arr1)
	arr2 := arr1
	arr2[1] = 11
	//(*arr2)[0] = 11
	fmt.Println("arr2 after change -> arr1:", arr1, "arr2:", arr2)
	fmt.Printf("arr1 address: %T, arr2 address: %T \n", arr1, arr2)
	fmt.Printf("arr1 address: %p, arr2 address: %p \n", &arr1, &arr2)

	slice1 := arr1[2:5]
	slice1[0] = 22
	fmt.Println("slice01:", slice1, "arr1:", arr1)

	b := []byte{'g', 'o', 'l', 'a', 'n', 'g'}
	str := string(b[1:4])
	fmt.Println(str, string(b[:2]), string(b[2:]), string(b[:]))
}

func ArraySample01() {
	var strArr = [5]string{"a", "b", "c", "d", "f"}
	for i := range strArr {
		fmt.Println("i ->", i, strArr[i])
	}

	var intArr = [3]int{}
	intArr[0] = 1
	intArr[1] = 2
	fmt.Println("len:", len(intArr), intArr)

	var arr3 = new([4]int)
	arr3[0] = 1
	arr3[1] = 2
	arr3[2] = 3

	fmt.Println("arr3:", arr3, *arr3)
	arr4 := arr3
	arr4[0] = 11
	fmt.Println("arr3 by arr4 change:", arr3)

	arr5 := *arr3
	arr5[0] = 111
	fmt.Println("arr3 by arr5 change:", arr3)
}
